var allTimes = 0;
angular.module('myApp', ['infinite-scroll']);
//控制图片大小
var max = document.documentElement.clientWidth;
function initImg(obj) {
	obj.parentNode.style.width = (max - 30) / 2.0 + "px";
	obj.parentNode.style.height = (max - 30) / 2.0 + "px";
}
function zhuanqu(obj) {
	obj.parentNode.style.height = (max - 31) / 3.0 + "px";
}

function dianpu($scope, $http) {
	var uid = readData(localStorage.account);
	var gid = readData(localStorage.guideId);		
	urlLogo = getUrl("logo");//店铺logo
	headerLogo = getHeader();
	paramsLogo = 'id=' + gid;
	$http({
		method: 'POST',
		url: urlLogo,
		headers: headerLogo,
		timeout: 10000,
		//cache: true,
		data: paramsLogo
	}).success(function(data, status, headers, config) {
		if (data.obj) {
			$scope.logophoto = data.obj.marketShop.logophoto;
			$scope.shopName = data.obj.marketShop.name;
		} else {
			$scope.logophoto = null;
			$scope.shopName = null;
		};
	}).error(function() {
		console.log("店铺logo网络链接异常，请稍后重试(e)");
		allTimes++;
	});
	//商品数量
	urlNum = getUrl("goodsNum");
	headerNum = getHeader();
	paramsNum = 'indexpage=1&pageSize=4&id=' + gid;
		/*$http({
			method : 'POST',
			url : urlNum,
			headers : headerNum,
			timeout : 10000,
			//cache: true,
			data : paramsNum
		}).success(function(data, status, headers, config) {
			if (data.obj) {
				$scope.goodsNum = data.obj;
			} else {
				$scope.goodsNum = null;
			};
		}).error(function() {
			if (allTimes == 0) {
				alert('网络链接异常，请稍后重试(e)');
			}
		});*/
		//商品分类
	urlSort = getUrl("73getCatalogs");
	headerSort = getHeader();
	paramsSort = 'id=' + gid;
	$http({
		method: 'POST',
		url: urlSort,
		headers: headerSort,
		timeout: 10000,
		//cache: true,
		data: paramsSort
	}).success(function(data, status, headers, config) {
		if (data.obj.cataloglist) {
			$scope.sorts = data.obj.cataloglist;
		} else {
			$scope.sorts = null;
		};
	}).error(function() {
		if (allTimes == 0) {
			console.log('商品分类网络链接异常，请稍后重试(e)');
		}
	});
	var divs = mui('.main_pn_con');
	var products = mui('.main_product_con');
	var pname = document.getElementById("name");

	$scope.jumpToBC = function(index) {
			$scope.goodsPromotion = [];
			var divs = document.getElementsByClassName("main_pn_con");
			divs[index].className = 'main_pn_con current';
			for (var i = 0; i < 4; i++) {
				if (i != index) {
					divs[i].className = 'main_pn_con';
					document.getElementById("name").innerText = "";
				}
			}
			switch (index) {
				case 0:
					pname.style.display = 'none';
					var as = document.getElementsByClassName("active");
					for (var i = 0; i < as.length; i++) {
						as[i].className = '';
					}
					document.getElementById("gundongtiao").style.display = 'block';
					$scope.all_products(index);
					break;
				case 1:
					pname.innerText = '最新上架';
					pname.style.display = 'block';
					document.getElementById("gundongtiao").style.display = 'none';
					$scope.all_products(index);
					break;
				case 2:
					pname.innerText = '爆款商品';
					pname.style.display = 'block';
					document.getElementById("gundongtiao").style.display = 'none';
					$scope.all_products(index);
					break;
				case 3:
					pname.innerText = '促销专区';
					pname.style.display = 'none';
					document.getElementById("gundongtiao").style.display = 'none';
					$scope.goodsAll = [];
					mui.toast('暂无促销商品~~');
					return;
					$scope.all_products(index);
					break;
			}
			plus.nativeUI.showWaiting('加载中...');
		}
		//全部商品
	$scope.all_products = function(index) {
			var lg = index + '';
			var ID = '';
			if (lg.length == 1) {
				if (index == 0) {
					allgoodsUrl = getUrl("44allGoods");
				}
				if (index == 1) {
					allgoodsUrl = getUrl("goodsNew");
				}
				if (index == 2) {
					allgoodsUrl = getUrl("67baokuan");
				}
				if (index == 3) {
					allgoodsUrl = getUrl("81promotionZone");
				}
				ID = gid;
			} else {
				ID = gid + '&catalogID=' + index;
				var as = document.getElementsByClassName("active");
				for (var i = 0; i < as.length; i++) {
					as[i].className = '';
				}
				document.getElementById(index).className = "active";
			}
			$scope.pages = 0;//默认加载第一页，初始值0			
			$scope.goodsAll = [];
			$scope.totalpage = 1;//总页数初始默认1，读取数据后再更新
			headerAll = getHeader();			
			$scope.loadMore = function() {
				if ($scope.pages < $scope.totalpage) {
					paramsAll = 'indexpage=' + $scope.pages + '&pageSize=6&id=' + ID;
					if (index == 3) {
						paramsAll = "userId=" + ID;
					}
					$scope.pages = $scope.pages + 1;
					$http({
						method: 'POST',
						url: allgoodsUrl,
						headers: headerAll,
						timeout: 10000,
						//cache: true,
						data: paramsAll
					}).success(function(data, status, headers, config) {
						//alert(JSON.stringify(data))
						if (data.obj) {
							setTimeout(function() {
								plus.nativeUI.closeWaiting();
							}, 200)
							if (index == 3) {
								$scope.goodsAll = [];
								$scope.goodsPromotion = data.obj.promotionList; //.slice(0,3)
								for (var i = 0; i < $scope.goodsPromotion.length; i++) {
									for (var j = 0; j < $scope.goodsPromotion[i].list.length; j++) {
										if (!$scope.goodsPromotion[i].list[j].picture) {
											$scope.goodsPromotion[i].list[j].picture = '../../image/middle.png';
										}
									}
								}
							} else {
								$scope.goodsPromotion = [];
								var goodsList = data.obj.list;
								for (var i = 0; i < goodsList.length; i++) {
									$scope.goodsAll.push(goodsList[i]);
								}
								for (var i = 0; i < $scope.goodsAll.length; i++) {
									$scope.goodsAll[i].height=(max - 30) / 2.0 + "px";
									if ($scope.goodsAll[i].ruleflag == 1) {
										$scope.goodsAll[i].cu = true;
									} else {
										$scope.goodsAll[i].cu = false;
									}
									if ($scope.goodsAll[i].pricename) {
										$scope.goodsAll[i].hide = true;
									} else {
										$scope.goodsAll[i].hide = false;
									}
									if (!$scope.goodsAll[i].picture) {
										$scope.goodsAll[i].picture = '../../image/middle.png';
									}
								}
							}
							$scope.totalpage = data.obj.totalpage;//下拉页面+1
							setTimeout(function() {
									echo.init({
										offset: '100px',
										throttle: 0
									});
								}, 0);								
						} else {
							plus.nativeUI.closeWaiting();
							$scope.goodsAll = [];
							$scope.pages = $scope.pages - 1;
						};

					}).error(function() {
						plus.nativeUI.closeWaiting();
						$scope.pages = $scope.pages - 1;
						if (allTimes == 0) {
							console.log('全部商品网络链接异常，请稍后重试(e)');
						}
					});
				}
			}
			$scope.loadMore();
	}		
	$scope.all_products(0);//默认加全部商品
	$scope.openCxhd = function(alls) {
		localStorage.promotionName = writeData(alls.promotionName);
		localStorage.promotionId = writeData(alls.promotionId);
		localStorage.ruleType = writeData(alls.promotionflag);
		var str = alls.promotionName;
		if (alls.promotionflag == 't_full_reduce_rule' || alls.promotionflag == 't_full_gift_rule') {
			mui.openWindow({
				url: 'promotionActive.html',
				id: 'promotionActive.html'
			});
		}
		if (alls.promotionflag == 't_activity') {
			mui.openWindow({
				url: 'promotionLimitTime.html',
				id: 'promotionLimitTime.html'
			});
		}
	}
	$scope.detail = function(id) {// 打开商品详情页面		
		localStorage.goodsId = writeData(id);
		mui.openWindow({
			url: 'productDetail.html',
			id: 'productDetail.html'
		});
	}
}

function openBuyCar() {// 打开购物车	
	mui.openWindow({
		url: 'buyCar.html',
		id: 'buyCar.html',
		waitting: {
			aotoShow: false
		}
	})
}

function openSearchProduct() {// 打开店铺商品搜索也
	mui.openWindow({
		url: 'productResult.html',
		id: 'productResult.html'
	})
}